﻿<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- saved from url=(0014)about:internet -->
<html xmlns:MSHelp="http://www.microsoft.com/MSHelp/" lang="en-us" xml:lang="en-us"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<meta name="DC.Type" content="topic">
<meta name="DC.Title" content="Lock Pathologies">
<meta name="DC.subject" content="Lock Pathologies">
<meta name="keywords" content="Lock Pathologies">
<meta name="DC.Relation" scheme="URI" content="../tbb_userguide/Mutual_Exclusion.htm">
<meta name="DC.Relation" scheme="URI" content="Atomic_Operations.htm">
<meta name="DC.Format" content="XHTML">
<meta name="DC.Identifier" content="tutorial_Lock_Pathologies">
<link rel="stylesheet" type="text/css" href="../intel_css_styles.css">
<title>Lock Pathologies</title>
<xml>
<MSHelp:Attr Name="DocSet" Value="Intel"></MSHelp:Attr>
<MSHelp:Attr Name="Locale" Value="kbEnglish"></MSHelp:Attr>
<MSHelp:Attr Name="TopicType" Value="kbReference"></MSHelp:Attr>
</xml>
</head>
<body id="tutorial_Lock_Pathologies">
 <!-- ==============(Start:NavScript)================= -->
 <script src="..\NavScript.js" language="JavaScript1.2" type="text/javascript"></script>
 <script language="JavaScript1.2" type="text/javascript">WriteNavLink(1);</script>
 <!-- ==============(End:NavScript)================= -->
<a name="tutorial_Lock_Pathologies"><!-- --></a>

 
  <h1 class="topictitle1">Lock Pathologies</h1>
 
   
  <div> 
	 <p>Locks can introduce performance and correctness problems. If you are new
		to locking, here are some of the problems to avoid: 
	 </p>
 
	 <div class="section"><h2 class="sectiontitle">Deadlock</h2> 
		 
		<p>Deadlock happens when threads are trying to acquire more than one
		  lock, and each holds some of the locks the other threads need to proceed. More
		  precisely, deadlock happens when: 
		</p>
 
		<ul type="disc"> 
		  <li> 
			 <p>There is a cycle of threads 
			 </p>
 
		  </li>
 
		  <li> 
			 <p>Each thread holds at least one lock on a mutex, and is waiting on
				a mutex for which the 
				<em>next</em> thread in the cycle already has a lock. 
			 </p>
 
		  </li>
 
		  <li> 
			 <p>No thread is willing to give up its lock. 
			 </p>
 
		  </li>
 
		</ul>
 
		<p>Think of classic gridlock at an intersection – each car has "acquired"
		  part of the road, but needs to "acquire" the road under another car to get
		  through. Two common ways to avoid deadlock are: 
		</p>
 
		<ul type="disc"> 
		  <li> 
			 <p>Avoid needing to hold two locks at the same time. Break your
				program into small actions in which each can be accomplished while holding a
				single lock. 
			 </p>
 
		  </li>
 
		  <li> 
			 <p>Always acquire locks in the same order. For example, if you have
				"outer container" and "inner container" mutexes, and need to acquire a lock on
				one of each, you could always acquire the "outer sanctum" one first. Another
				example is "acquire locks in alphabetical order" in a situation where the locks
				have names. Or if the locks are unnamed, acquire locks in order of the mutex’s
				numerical addresses. 
			 </p>
 
		  </li>
 
		  <li> 
			 <p>Use atomic operations instead of locks. 
			 </p>
 
		  </li>
 
		</ul>
 
	 </div>
 
	 <div class="section"><h2 class="sectiontitle">Convoying</h2> 
		 
		<p>Another common problem with locks is 
		  <em>convoying</em>. Convoying occurs when the operating system interrupts
		  a thread that is holding a lock. All other threads must wait until the
		  interrupted thread resumes and releases the lock. Fair mutexes can make the
		  situation even worse, because if a waiting thread is interrupted, all the
		  threads behind it must wait for it to resume. 
		</p>
 
		<p>To minimize convoying, try to hold the lock as briefly as possible.
		  Precompute whatever you can before acquiring the lock. 
		</p>
 
		<p>To avoid convoying, use atomic operations instead of locks where
		  possible. 
		</p>
 
	 </div>
 
  </div>
 
  
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong>&nbsp;<a href="../tbb_userguide/Mutual_Exclusion.htm">Mutual Exclusion</a></div>
</div>
<div class="See Also">
<h2>See Also</h2>
<div class="linklist">
<div><a href="Atomic_Operations.htm">Atomic Operations
		  </a></div></div>
</div> 

</body>
</html>
